package com.xbdun.web.validatebmp.utils.http;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 控制器工具类
 */
public class ControllerUtil {
	private static Log log=LogFactory.getLog(ControllerUtil.class);

	private ControllerUtil() {
	}
	/**
	 * 打印XML
	 * produces={"text/xml;charset=UTF-8"}
	 *@param response {@link HttpServletResponse}
	 *@param content     xml内容
	 *@throws IOException
	 */
	public static void printXml(HttpServletResponse response, String content)
			throws IOException {
		log.debug(content);
		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(content);
	}
	/**
	 * 打印Json
	 * produces={"application/json;charset=UTF-8"}
	 *@param response {@link HttpServletResponse}
	 *@param content  Json内容
	 *@throws IOException
	 */
	public static void printJson(HttpServletResponse response, String content)
			throws IOException {
		log.debug(content);
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(content);
	}
	/**
	 * 打印HTML
	 * produces={"text/html;charset=UTF-8"}
	 *@param response {@link HttpServletResponse}
	 *@param content  HTML内容
	 *@throws IOException
	 */
	public static void printHtml(HttpServletResponse response, String content)
			throws IOException {
		log.debug(content);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(content);
	}
	
	/**
	 * 获取当前项目的绝对地址
	 *@param request {@link HttpServletRequest}
	 *@return        当前项目的绝对地址
	 */
	public static String getRealPath(HttpServletRequest request){
		return request.getSession().getServletContext().getRealPath("/");
	}
	/**
	 * 显示请求参数
	 *@param request {@link HttpServletRequest}
	 */
	public static void showRequestParameters(HttpServletRequest request) {
		Enumeration<?> paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = paramNames.nextElement().toString();
			String paramValue = request.getParameter(paramName);
			log.info(paramName + ":" + paramValue);
		}
	}
	/**
	 * 创建spring重定向地址视图
	 *@param url 原始url地址
	 *@return    spring视图
	 */
	public static String createRedirectView(String url){
		return "redirect:"+url;
	}
	
	/**
	 * 获取客户端的IP地址
	 * @return
	 */
	public final static String getIpAddr(HttpServletRequest currentRequest) {
		String ipAddress = null;
		if(null!=currentRequest){
			ipAddress = currentRequest.getHeader("x-forwarded-for");
			if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
				ipAddress = currentRequest.getHeader("Proxy-Client-IP");
			}
			if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
				ipAddress = currentRequest.getHeader("WL-Proxy-Client-IP");
			}
			if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
				ipAddress = currentRequest.getRemoteAddr();
				if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
					// 根据网卡取本机配置的IP
					InetAddress inet = null;
					try {
						inet = InetAddress.getLocalHost();
					} catch (UnknownHostException e) {
						e.printStackTrace();
					}
					ipAddress = inet.getHostAddress();
				}
	
			}
			// 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
			if (ipAddress != null && ipAddress.length() > 15) {
				if (ipAddress.indexOf(",") > 0) {
					ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
				}
			}
		}
		return ipAddress;
	}
}
